<template>
  <div class="app-container">
    <el-form ref="form" :model="borrow" :rules="rulesBro" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="申请人" prop="userName">
            <el-input v-model="borrow.userName" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单位" prop="workUnit">
            <el-input v-model="borrow.workUnit" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="借阅人岗位" prop="postName">
            <el-input v-model="borrow.postName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="部门" prop="deptName">
            <el-input v-model="borrow.deptName" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="利用目的" prop="purpose">
            <el-input type="textarea" v-model="borrow.purpose" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="借阅说明" prop="borDetail">
            <el-input type="textarea" v-model="borrow.borDetail" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="借阅方式">
            <el-checkbox v-model="check_online">电子借阅</el-checkbox>
            <el-checkbox v-model="check_offline">实体借阅</el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="借阅天数" prop="dayNum">
            <el-input-number v-model="borrow.dayNum" controls-position="right" :min="1" />
          </el-form-item>
        </el-col>
      </el-row>
      <!----------------------------- 文件借阅车部分 ---------------------------->
      <el-divider content-position="left">借阅车数据</el-divider>
      <el-table :data="borcartListFile" highlight-current-row border>
        <!-- 可替换部分开始 -->
        <el-table-column label="题名" align="center" prop="fileTitle" show-overflow-tooltip/>
        <el-table-column label="档案名称" align="center" prop="typeName" />
        <el-table-column label="档号" align="center" prop="docNum" />
        <el-table-column label="全宗号" align="center" prop="fullAnces" />
        <el-table-column label="文件类型" align="center" prop="tbType" >
           <template slot-scope="scope">
            <span v-if="scope.row.tbType === '1'">案卷文件</span>
            <span v-if="scope.row.tbType === '2'">卷内文件</span>
            <span v-if="scope.row.tbType === '3'">独立文件</span>
          </template>
        </el-table-column>
        <!-- 可替换部分结束 -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-tag @click="handleDeleteFile(scope.row)" type="danger" class="point">移除</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin-top:20px">
        <el-col>
          <el-button type="primary" size="mini" @click="submit">提交</el-button>
          <el-button type="danger" size="mini" @click="close">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getInfo } from "@/api/login";
import {
  listBorrow,
  getBorrow,
  delBorrow,
  addBorrow,
  updateBorrow,
  updateApprove,
  updateReject
} from "@/api/system/borrow";
import {
  listBorcart,
  getBorcart,
  delBorcart,
  addBorcart,
  updateBorcart
} from "@/api/system/borcart";

export default {
  data() {
    return {
      /***************借阅车内容********************/
      // 借阅
      borrow: {
        // 申请人
        userName: null,
        // 单位
        workUnit: null,
        // 部门
        deptName: null,
        // 借阅人岗位
        postName: null,
        // 借阅说明
        borDetail: null,
        // 利用目的
        purpose: null,
        // 电子借阅。0，否，1，是
        online: null,
        // 实体借阅。0，否，1，是
        offline: null,
        // 借阅天数
        dayNum: 1
      },
      // 用户ID
      userId: null, 
      // 借阅方式单选框
      check_online: true,
      check_offline: false,
      // 文件级借阅车表格数据
      borcartListFile: [],
      // 查询参数
      queryParams: {},
      rulesBro: {
        userName: [
          { required: true, message: "申请人不能为空", trigger: "blur" }
        ],
        workUnit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        dayNum: [
          { required: true, message: "借阅天数不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    getInfo().then(res => {
      this.userId = res.user.userId;
      this.getListCart(); // 按人员ID获取借阅车数据
    });
  },

  methods: {
    /*************************借阅车内容***********************/
    // 提交
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.check_online && !this.check_offline) {
            this.$message.error("请选择借阅方式！");
          } else if (
            this.borcartListFile.length == 0
          ) {
            this.$message.error("请先添加借阅车！");
          } else {
            this.borrow.online = this.check_online ? 1 : 0;
            this.borrow.offline = this.check_offline ? 1 : 0;
            this.borrow.borcartList = this.borcartListFile;
            addBorrow(this.borrow).then(response => {
              this.$message({
                type: 'success',
                message: '新增成功',
                duration: 1000
              });
              this.$store.commit("SET_CART_NUM", 0);
              this.getListCart();
            });
          }
        }
      });
    },
    // 关闭
    close() {
      this.$router.push({ path: "/client/home" });
    },
    // 移除文件级
    handleDeleteFile(row) {
      const cartIds = row.cartId || this.ids;
      this.$modal
        .confirm('是否确认删除借阅车编号为"' + cartIds + '"的数据项？')
        .then(function() {
          return delBorcart(cartIds);
        })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功',
            duration: 1000
          });
          const cartNum = this.$store.state.borcart.cartNum - 1;
          this.$store.commit("SET_CART_NUM", cartNum);
          this.getListCart();
        })
        .catch(() => {});
    },
    // 获取文件级借阅车数据
    getListCart() {
      this.loading = true;
      this.queryParams.userId = this.userId;
      listBorcart(this.queryParams).then(response => {
        this.borcartListFile = response.rows;
        this.loading = false;
      });
    }
  }
};
</script>

<style lang="scss" scoped >
.point {
  cursor: pointer;
}
</style>